/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;


class TextRenderer {
	static private BufferedImage image = new BufferedImage(512, 512, 
			BufferedImage.TYPE_BYTE_GRAY);

	private TextRenderer() {
	}

	static public GlyphImage renderGlyph(Font font, 
			AffineTransform transform, int glyph) {
		int glyphCodes[] = {glyph};
		Graphics2D graphics = image.createGraphics();
		
		graphics.addRenderingHints(new RenderingHints(
				RenderingHints.KEY_ANTIALIASING, 
				RenderingHints.VALUE_ANTIALIAS_ON));
		graphics.addRenderingHints(new RenderingHints(
				RenderingHints.KEY_FRACTIONALMETRICS, 
				RenderingHints.VALUE_FRACTIONALMETRICS_OFF));
		graphics.setColor(new Color(0.0f, 0.0f, 0.0f));
		graphics.fillRect(0, 0, 512, 512);
		graphics.setTransform(transform);

		FontRenderContext frc = graphics.getFontRenderContext();
		GlyphVector gv = font.createGlyphVector(
				frc,
				glyphCodes);
		Rectangle r = gv.getGlyphPixelBounds(0, frc, 0, 0);
		GlyphImage gi = new GlyphImage();
		Point2D p;
		
		graphics.setColor(new Color(1.0f, 1.0f, 1.0f));
		try {
			p = transform.createInverse().transform(new Point2D.Double(-r.x, -r.y), null);
		} catch(NoninvertibleTransformException e) {
			p = new Point2D.Double(0, 0);
		}
		graphics.drawGlyphVector(gv, (float)p.getX(), (float)p.getY());
		
		int pixels[] = new int[r.width * r.height];
		
		image.getRaster().getPixels(0, 0, r.width, r.height, pixels);
		
		gi.width = r.width;
		gi.height = r.height;
		gi.format = Format.AL8;
		gi.data = new byte[r.width * r.height];
		gi.x = -r.x;
		gi.y = -r.y;
		
		for(int i = 0; i < r.width * r.height; i++)
			gi.data[i] = (byte)pixels[i];
				
		return gi;
	}
	
	static public java.awt.Shape getGlyphOutline(Font font, 
			AffineTransform transform, int glyph) {
		int glyphCodes[] = {glyph};
		Graphics2D graphics = image.createGraphics();
		
		graphics.addRenderingHints(new RenderingHints(
				RenderingHints.KEY_ANTIALIASING, 
				RenderingHints.VALUE_ANTIALIAS_ON));
		graphics.addRenderingHints(new RenderingHints(
				RenderingHints.KEY_FRACTIONALMETRICS, 
				RenderingHints.VALUE_FRACTIONALMETRICS_OFF));
		graphics.setTransform(transform);

		FontRenderContext frc = graphics.getFontRenderContext();
		GlyphVector gv = font.createGlyphVector(
				frc,
				glyphCodes);
		
		return gv.getGlyphOutline(0);
	}
}
